package com.oly.cms.general.service.search;

import java.util.List;

import com.oly.cms.common.model.properties.OlyWebConfigProetries;
import com.oly.cms.general.mapper.WebSearchMapper;
import com.oly.cms.general.model.po.WebCat;
import com.oly.cms.general.service.IWebSearchService;
import com.oly.cms.general.utils.tree.CategoryTreeUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.config.service.ISysConfigService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WebCategoryServiceImpl implements IWebSearchService {
    @Autowired
    private WebSearchMapper webSearchMapper;

    @Autowired
    private ISysConfigService configService;

    public WebCat getWebCatByCatId(long catId) {

        return webSearchMapper.getWebCatByCatId(catId);
    }

    /**
     * 获取分类列表
     * 
     * @param webCat
     * @return
     */
    public List<WebCat> listWebCats(WebCat webCat) {
        return webSearchMapper.listWebCats(webCat);
    }

    public List<WebCat> listWebCatsByType(Integer catType, Long catId) {
        WebCat webCat = new WebCat();
        webCat.setCatType(catType);
        webCat.setCatId(catId);
        return webSearchMapper.listWebCats(webCat);
    }

    public List<WebCat> listWebCatsByVisible(Integer visible, Long catId, String themeName) {
        WebCat webCat = new WebCat();
        webCat.setVisible(visible);
        webCat.setCatId(catId);
        setSupportType(webCat, themeName);
        return this.listWebCats(webCat);
    }

    public List<WebCat> listWebCatsById(Long catId, String themeName) {
        WebCat webCat = new WebCat();
        webCat.setCatId(catId);
        setSupportType(webCat, themeName);
        return this.listWebCats(webCat);
    }

    public List<WebCat> listWebCatsByParentId(Long catId, String themeName) {
        WebCat webCat = new WebCat();
        webCat.setParentId(catId);
        setSupportType(webCat, themeName);
        return this.listWebCats(webCat);
    }

    public WebCat listWebCatsTreeById(Long catId, String themeName) {
        return CategoryTreeUtils.getCatTree(this.listWebCatsById(catId, themeName), catId);
    }

    public WebCat listWebCatsTree(WebCat webCat) {
        if (webCat != null && webCat.getCatId() != null) {
            webCat.setCatId(0L);
        }
        return CategoryTreeUtils.getCatTree(this.listWebCats(webCat), webCat.getCatId());
    }

    private void setSupportType(WebCat webCat, String themeName) {
        String supportType = configService.selectConfigDefauleValue(themeName, OlyWebConfigProetries.ARTICLE_TYPES);
        if (StringUtils.isNotEmpty(supportType)) {
            webCat.getParams().put("supportType", supportType);
        }

    }
}
